-- Copyright (c) 2016 Lucky Byte, Inc.
--
-- Summary : 导入银联业务卡 BIN 数据
--
-- Author : xiaohu <xiaohu@lucky-byte.com>
-- Since  : 2016.8.3
--
-- Reference
--  006.3 - 中国银联银行卡系统技术规范 第三部分 文件接口规范
--
-- History
--  2016.08.03 : 建立此文件
--
local _load_file = require 'upay.dbload.loadfile'

-- 解析单行数据, 以 table 返回解析后的数据，或 nil
--
local _split_ywk_line = function(filepath, line_num, line)
    if string.len(line) < 95 then
        logger.warn('YWKBIN 文件[%s]第[%s]行数据[%s]长度[%d]不足95，忽略之.',
            filepath, line_num, line, string.len(line))
        return nil
    end
    local _retval = {
        card_type           = 4,
        issuer_code         = string.trim(string.sub(line, 1, 11)),
        issuer_name         = string.trim(gb18030.str(string.sub(line, 13, 42))),
        card_no_len         = string.trim(string.sub(line, 44, 45)),
        bin_num             = string.trim(string.sub(line, 47, 58)),
        cup_logo_flag       = string.trim(string.sub(line, 60, 60)),
        debit_credit_flag   = string.trim(string.sub(line, 62, 62)),
        add_delete_stat     = string.trim(string.sub(line, 64, 64)),
        additional_data     = string.trim(string.sub(line, 66, 95)),
        usage_index         = 0,
    }
    _retval.bin_len = string.trimlen(_retval.bin_num)
    return _retval
end

-- 入口函数
--
local _load_ywkbin = function(filepath)
    return _load_file({
        filepath    = filepath,
        table_name  = 'pay_cardbin',
        line_split  = _split_ywk_line,
        unique_keys = { 'card_type', 'bin_num', 'bin_len', 'card_no_len' }
    })
end

return _load_ywkbin
